﻿//var _url = "http://192.168.0.226:8080";
//var imgUrl = "http://192.168.0.226:8080/money";
var _url = "http://47.92.213.93:8080";
var imgUrl = "http://47.92.213.93:8080/money";

var _load = {
	"open": function() {
		var loadingStr = '<div id="loading">';
		loadingStr += '<div id="loading-center">';
		loadingStr += '<div id="loading-center-absolute">';
		loadingStr += '<div class="loadingObject" id="loadingObject_four"></div>';
		loadingStr += '<div class="loadingObject" id="loadingObject_three"></div>';
		loadingStr += '<div class="loadingObject" id="loadingObject_two"></div>';
		loadingStr += '<div class="loadingObject" id="loadingObject_one"></div>';
		loadingStr += '</div></div></div>';
		$("body").append(loadingStr);
	},
	"remove": function() {
		var loadingObj = $("#loading");
		if(loadingObj) {
			loadingObj.fadeOut(500);
			setTimeout(function() {
				loadingObj.remove();
			}, 800);
		}
	}
}

var local = {
	"saveData": function(key, val) {
		localStorage.setItem(key, JSON.stringify(val));
	},
	"getData": function(key) {
		var data = localStorage.getItem(key);
		if(data != null) {
			return(JSON.parse(data));
		} else {
			return null;
		}
	},
	"removeData": function(key) {
		localStorage.removeItem(key);
	}
}

var _user = {
	"setUserInfo": function(rs) {
		if(!rs) {
			return;
		}
		local.saveData("yb_user_info", rs.data);
	},
	"getUserToken": function() {
		var userInfo = local.getData("yb_user_info");
		if(userInfo) {
			return userInfo.token;
		} else {
			this.signOut();
		}
	},
	"getUserInfo": function() {
		var userInfo = local.getData("yb_user_info");
		if(userInfo) {
			return userInfo;
		} else {
			this.signOut();
		}
	},
	"signOut": function() {
		local.removeData("yb_user_info");
		window.location.href = "../login/login.html";
		local.removeData("loginInfo");
	}
}

/* 去空白 */
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/gm, "");
};

function isNull(val) {
	if(val == "" || val == null || val == -1) {
		return true;
	} else {
		return false;
	}
}

// 通过URL获取Get参数
function getQueryParams(url) {
	var url = url ? url : decodeURIComponent(window.location.href);
	var pos = url.indexOf("?");
	var params = {};
	if(pos != -1 && pos < url.length - 1) {
		var q = url.substr(pos + 1);
		var arr = q.split("&");
		var cur;
		for(var i = 0; i < arr.length; i++) {
			cur = arr[i].split("=");
			params[cur[0]] = cur[1];
		}
	}
	return params;
}

function isReqSucceed(rs, fun) {
	var status = false;
	switch(rs.code) {
		case -1:
			break;
		case 0:
			break;
		case 1:
			status = true;
			break;
		case 2:
			status = true;
			_user.signOut();
			break;
		default:
			break;
	}
	if(!status) {
		$.alert(rs.msg, "信息", fun);
		return false;
	}
	return true;
}

/**
 * 方法说明：(表单请求)ajax封装
 * @param {Object} url      请求路径
 * @param {Object} data     请求参数：例如{"id":1}
 * @param {Object} success  回调函数 或者 回调函数名
 * @param {Object} type     请求类型
 * @param {Object} cache    浏览器历史缓存
 * @param {Object} alone    独立提交（一次有效的提交）
 * @param {Object} async    异步请求  ： true | false
 * @param {Object} dataType 接收数据类型  json
 * @param {Object} error    错误处理
 */
var ajaxStatus = true;

function AjaxRequest(options) {
	var load = options.load;
	var headers = options.headers || true;
	var ajaxform = options.ajaxform || false;
	var type = options.type || 'post';
	var dataType = options.dataType || 'json';
	var contentType = options.contentType || 'application/x-www-form-urlencoded; charset=UTF-8';
	var async = true;
	if(options.async == false) {
		async = false;
	}
	var alone = options.alone || false;
	var cache = options.cache || false;
	var load = options.load || false;
	var upload = options.upload || false;
	var success = function(data) {
		if(typeof(options.success) == 'function') {
			options.success(data);
		}
	};
	var error = error || function(data) {
		$.alert("[" + data.status + "]服务器繁忙", "服务器异常");
		ajaxStatus = true;
	};
	/*判断是否可以发送请求*/
	if(!ajaxStatus) {
		return false;
	}
	ajaxStatus = false; //禁用ajax请求
	/*正常情况下1秒后可以再次多个异步请求，为true时只可以有一次有效请求（例如添加数据）*/
	if(!alone) {
		ajaxStatus = true;
	}
	var reqObj = {
		'complete': function() {
			//_load.remove();
			$.hideLoading();
		},
		'url': options.url,
		'data': options.data,
		'type': type,
		'dataType': dataType,
		'contentType': contentType,
		'async': async,
		'success': success,
		'error': error,
		'jsonpCallback': 'jsonp' + (new Date()).valueOf().toString().substr(-4),
	};
	if(ajaxform) {
		reqObj.data = JSON.stringify(options.data);
		reqObj.contentType = "application/json;charset=UTF-8";
	}
	if(upload) {
		reqObj["processData"] = false;
		reqObj["contentType"] = false;
	}
	if(typeof(headers) == 'function') {
		reqObj["headers"] = headers;
	} else if(headers == "addToken") {
		reqObj.headers = {
			"authorization": _user.getUserToken()
		}
	}
	//	if($("#loading").length == 0 && load == true) {
	//		_load.open();
	//	}
	if(load == true) {
		$.showLoading();
	}
	$.ajax(reqObj);
}

/*部分隐藏处理*/
function dealWithHide(str, frontLen, endLen) {
	var len = str.length - frontLen - endLen;
	var asterisk = '';
	for(var i = 0; i < len; i++) {
		asterisk += '*';
	}
	return str.substring(0, frontLen) + asterisk + str.substring(str.length - endLen);
}

// 暴露方法到window上
function exposeGlobalMethods(methods) {
	for(var k in methods) {
		window[k] = methods[k];
	}
}

/*时间渲染*/
function fixNumWithZero(m) {
	return m < 10 ? '0' + m : m
}

function getFormatTime(times) {
	var time = new Date(times);
	var y = time.getFullYear();
	var m = time.getMonth() + 1;
	var d = time.getDate();
	var h = time.getHours();
	var i = time.getMinutes();
	var s = time.getSeconds();
	return {
		"y": y,
		"m": fixNumWithZero(m),
		"d": fixNumWithZero(d),
		"h": fixNumWithZero(h),
		"i": fixNumWithZero(i),
		"s": fixNumWithZero(s)
	};
}
// 时间戳的转换
function getLocalTime(s) {
	if(!s) {
		return "";
	}
	var opts = getFormatTime(s);
	return opts.y + "-" + opts.m + "-" + opts.d + " " + opts.h + ":" + opts.i + ":" + opts.s;
}

function getLocalDate(s) {
	if(!s) {
		return "";
	}
	var opts = getFormatTime(s);
	return opts.y + "-" + opts.m + "-" + opts.d;
}

function getLocalTimeByCn(s) {
	if(!s) {
		return "";
	}
	var opts = getFormatTime(s);
	return opts.y + "年" + opts.m + "月" + opts.d + "日" + " " + opts.h + ":" + opts.i + ":" + opts.s;
}

function getLocalDateByCn(s) {
	if(!s) {
		return "";
	}
	var opts = getFormatTime(s);
	return opts.y + "年" + opts.m + "月" + opts.d + "日";
}

//数组中根据某个值排序
function compare(property) {
	return function(a, b) {
		var value1 = a[property];
		var value2 = b[property];
		return value1 - value2;
	}
}

function harvestersanimation() {
	var html = '<div class="harvestersanimation" style="position: fixed;width: 100%;max-width: 750px;top: 0;bottom: 0;z-index: 9999;">' +
		'<img style="width: 100%;" src="../public/images/COIN.gif" /></div>';
	$("body").append(html);
	setTimeout(function() {
		$(".harvestersanimation").remove();
	}, 1600)
}

function harvestersanimationother() {
	var html = '<div class="harvestersanimation" style="position: fixed;width: 100%;max-width: 750px;left:-0.15rem;bottom: 1.3rem;z-index: 9999;">' +
		'<img style="width: 100%;" src="../public/images/COIN.gif" /></div>';
	$("body").append(html);
	setTimeout(function() {
		$(".harvestersanimation").remove();
	}, 1600)
}
//获取指定的get参数
function getUrlParam(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); // 构造一个含有目标参数的正则表达式对象
	var r = window.location.search.substr(1).match(reg); // 匹配目标参数
	if(r != null) return unescape(r[2]);
	return null; // 返回参数值
};

/* 保留指定位数小数 */
function toDecimal(v, e) {
	var t = 1;
	for(; e > 0; t *= 10, e--);
	for(; e < 0; t /= 10, e++);
	return Math.round(v * t) / t;
}

function rechargeSuccessCallback() {
	window.location.href = "../../money/index/rechargeSuccess.html";
}

//
//function withdrawValidatorCallback(uuid, money, token) {
//	AjaxRequest({
//		url: _url + "/money/wallet/drawmoney",
//		type: "post",
//		load: true,
//		data: {
//			"userid": uuid,
//			"money": money,
//			"sign": token
//		},
//		success: function(rs) {
//			if(isReqSucceed(rs)) {}
//		}
//	});
//}